package com.blue.wechat.invoke.spi.custom;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.blue.core.dict.HttpMethod;
import com.blue.wechat.cache.Wechat;
import com.blue.wechat.invoke.BaseInvoker;
import com.blue.wechat.invoke.InvokeException;
import com.blue.wechat.invoke.WechatInvokeUtil;
import com.blue.wechat.invoke.WechatInvoker;
import com.blue.wechat.invoke.annotation.WechatUrl;
import com.blue.wechat.invoke.model.ErrorMsg;
import com.blue.wechat.invoke.model.custom.AccountImg;

/**
 * 上传客服帐号头像
 * 
 * @author zhengj
 * @since 1.0 2017年1月28日
 */
@WechatUrl(WechatInvoker.UPLOAD_CUSTOM_ACCOUNT_IMG)
public class UploadCustomAccountImgInvoker extends BaseInvoker<ErrorMsg>
{
	public UploadCustomAccountImgInvoker()
	{
	}

	@Override
	protected WechatInvoker getInvoker()
	{
		return WechatInvoker.UPLOAD_CUSTOM_ACCOUNT_IMG;
	}
	
	@Override
	protected String invoke(String url, HttpMethod method, Object post) throws Exception
	{
		File file = null;
		if (post instanceof File)
		{
			file = (File)post;
		}
		if (post instanceof AccountImg)
		{
			AccountImg img = (AccountImg)post;
			file = img.getFile();
		}
		if (file == null)
			throw new InvokeException("上传客服帐号头像不能为空");
		
		Map<String, File> fileParam = new HashMap<>();
		fileParam.put(file.getName(), file);
		String resp = httpInvoker.upload(url, null, fileParam);
		return resp;
	}

	@Override
	protected ErrorMsg handleSuccess(String resp, Wechat wechat)
	{
		ErrorMsg msg = WechatInvokeUtil.toResponse(resp, ErrorMsg.class);
		return msg;
	}
}
